package gncom.qr;

import java.util.Vector;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class InfoPage extends ListActivity {

	private Button gotoNoticeBtn;
	private Button gotoGuideBtn;
	private int flag_btn = 1;
	private ViewFlipper vf;
	private String[] noticeTitle;
	private String[] noticeContent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info_page);
		
		ParseJSON board_json = new ParseJSON();
		board_json.executeHttpPost_board();
		Vector<ParseJSON_VO_board> list = board_json.getParse();
		
		noticeTitle = new String[list.size()];
		noticeContent = new String[list.size()];
		
		for (int i = 0; i < list.size(); i++) {
			ParseJSON_VO_board vo = list.get(i);
			noticeTitle[i] = vo.getSubject() +"\n"+  vo.getWdate();
//			noticeContent[i] = "hit : " + vo.getHit() + "\nNO : "+vo.getNo() + "\nwriter : " + vo.getWriter();
			noticeContent[i] = vo.getContent();
			Log.i("http", noticeTitle[i]);
		}

		setListAdapter(new SpeechListAdapter(this));

		vf = (ViewFlipper) findViewById(R.id.viewFlipper1);

		gotoNoticeBtn = (Button) findViewById(R.id.gotoNoticeBtn);
		gotoGuideBtn = (Button) findViewById(R.id.gotoGuideBtn);
		if (flag_btn == 1) {
			gotoNoticeBtn.setEnabled(false);
		}

		gotoNoticeBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				vf.showNext();
				gotoGuideBtn.setEnabled(true);
				gotoNoticeBtn.setEnabled(false);
			}
		});

		gotoGuideBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				vf.showNext();
				gotoGuideBtn.setEnabled(false);
				gotoNoticeBtn.setEnabled(true);
				// startActivity(new Intent(getApplicationContext(),
				// Example.class).putExtra("story", story));
			}
		});
		
		
		
//		for(ParseJSON_VO_board vo:list){
//			Log.i("http", "hit : " + vo.getHit());
//			Log.i("http", "subject : " + vo.getSubject());
//			Log.i("http", "no : " + vo.getNo());
//			Log.i("http", "wdate : " + vo.getWdate());
//			Log.i("http", "writer : " + vo.getWriter());
//		}
		

	}// end onCreate()

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		((SpeechListAdapter) getListAdapter()).toggle(position);
	}

	private class SpeechListAdapter extends BaseAdapter {
		public SpeechListAdapter(Context context) {
			mContext = context;
		}

		public int getCount() {
			return mTitles.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			SpeechView sv;
			if (convertView == null) {
				sv = new SpeechView(mContext, mTitles[position],
						mDialogue[position], mExpanded[position]);
			} else {
				sv = (SpeechView) convertView;
				sv.setTitle(mTitles[position]);
				sv.setDialogue(mDialogue[position]);
				sv.setExpanded(mExpanded[position]);
			}
			return sv;
		}

		public void toggle(int position) {
			mExpanded[position] = !mExpanded[position];
			notifyDataSetChanged();
		}

		private Context mContext;
		
		

		private String[] mTitles = noticeTitle;
		private String[] mDialogue = noticeContent;
		private boolean[] mExpanded = { false, false, false, false, false,
				false, false, false };
	}

	private class SpeechView extends LinearLayout {
		private int[] mArrows = { R.drawable.arrow01, R.drawable.arrow02 };

		public SpeechView(Context context, String title, String dialogue,
				boolean expanded) {
			super(context);

			this.setOrientation(VERTICAL);
			// this.setBackgroundResource(R.drawable.notice_bg);
			this.setBackgroundColor(Color.rgb(30, 30, 30));

			LinearLayout top = new LinearLayout(context);
			top.setOrientation(HORIZONTAL);
			top.setBackgroundResource(R.drawable.story_info_box);

			LinearLayout lin01 = new LinearLayout(context);
			lin01.setOrientation(HORIZONTAL);
			lin01.setBackgroundResource(R.drawable.story_info_box);
			lin01.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			mArrow = new ImageView(context);
			mArrow.setImageResource(mArrows[0]);

			lin01.addView(mArrow);

			mTitle = new TextView(context);
			mTitle.setPadding(10, 15, 15, 0);
			mTitle.setTextColor(Color.WHITE);
			mTitle.setTextSize(16);
			mTitle.setText(title);

			lin01.addView(mTitle);

			// Button storyBtn = new Button(context);
			// storyBtn.setText("글쓰기");
			// lin01.addView(storyBtn);

			addView(lin01);

			mDialogue = new TextView(context);
			mDialogue.setPadding(30, 15, 0, 15);
			mDialogue.setTextSize(15);
			mDialogue.setText(dialogue);
			mDialogue.setTextColor(Color.WHITE);
			addView(mDialogue, new LinearLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

			mDialogue.setVisibility(expanded ? VISIBLE : GONE);

			System.out.println(mDialogue.getVisibility());
		}

		public void setTitle(String title) {
			mTitle.setText(title);
		}

		public void setDialogue(String words) {
			mDialogue.setText(words);
		}

		public void setExpanded(boolean expanded) {
			mDialogue.setVisibility(expanded ? VISIBLE : GONE);
			mArrow.setImageResource(expanded ? mArrows[1] : mArrows[0]);
		}

		private ImageView mArrow;
		private TextView mTitle;
		private TextView mDialogue;
	}
}